package com.gxuwz.easyschool;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.gxuwz.easyschool.fragment.MessageFragment;
import com.gxuwz.easyschool.fragment.MyFragment;
import com.gxuwz.easyschool.fragment.NavHomeFragment;
import com.gxuwz.easyschool.fragment.SchoolCardFragment;

public class HomeActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private NavHomeFragment navHomeFragment;
    private SchoolCardFragment schoolCardFragment;
    private MessageFragment messageFragment;
    private MyFragment myFragment;
    private ImageView home_page,school_card,message_page,my_page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        clickEvent();

    }
    private void init(){
        fragmentManager = getSupportFragmentManager();
        navHomeFragment = (NavHomeFragment) fragmentManager.findFragmentById(R.id.home_center);
        home_page = findViewById(R.id.home_page);
        school_card = findViewById(R.id.school_card);
        message_page = findViewById(R.id.message_page);
        my_page = findViewById(R.id.my_page);
        home_page.setBackgroundResource(R.drawable.home_active_icon);
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        navHomeFragment = new NavHomeFragment().getInstance();
        fragmentTransaction.replace(R.id.home_center,navHomeFragment);
        fragmentTransaction.commit();
    }

    private void clickEvent(){
        home_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                home_page.setBackgroundResource(R.drawable.home_active_icon);
                school_card.setBackgroundResource(R.drawable.school_card_icon);
                message_page.setBackgroundResource(R.drawable.message_icon);
                my_page.setBackgroundResource(R.drawable.character_icon);
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                navHomeFragment = new NavHomeFragment().getInstance();
                fragmentTransaction.replace(R.id.home_center,navHomeFragment);
                fragmentTransaction.commit();
            }
        });
        school_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                school_card.setBackgroundResource(R.drawable.school_card_active_icon);
                home_page.setBackgroundResource(R.drawable.home_icon);
                message_page.setBackgroundResource(R.drawable.message_icon);
                my_page.setBackgroundResource(R.drawable.character_icon);
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                schoolCardFragment = new SchoolCardFragment().getInstance();
                fragmentTransaction.replace(R.id.home_center,schoolCardFragment);
                fragmentTransaction.commit();
            }
        });
        message_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message_page.setBackgroundResource(R.drawable.message_active_icon);
                home_page.setBackgroundResource(R.drawable.home_icon);
                school_card.setBackgroundResource(R.drawable.school_card_icon);
                my_page.setBackgroundResource(R.drawable.character_icon);
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                messageFragment = new MessageFragment().getInstance();
                fragmentTransaction.replace(R.id.home_center,messageFragment);
                fragmentTransaction.commit();
            }
        });
        my_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                my_page.setBackgroundResource(R.drawable.my_active_icon);
                home_page.setBackgroundResource(R.drawable.home_icon);
                school_card.setBackgroundResource(R.drawable.school_card_icon);
                message_page.setBackgroundResource(R.drawable.message_icon);
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                myFragment = new MyFragment().getInstance();
                fragmentTransaction.replace(R.id.home_center,myFragment);
                fragmentTransaction.commit();
            }
        });
    }

}